<template>
	<view>
	    <view class="layout-form">
			<view>
				<uni-forms ref="baseForm" :modelValue="formData" label-position="top">
					<uni-forms-item label="旧密码" required>
						<uni-easyinput type="password" v-model="formData.oldPassword" placeholder="" />
					</uni-forms-item>
					<uni-forms-item label="新密码" required>
						<uni-easyinput type="password" v-model="formData.newPassword" placeholder="" @input="validatePassword"/>
					</uni-forms-item>
					<uni-forms-item label="确认密码" required>
						<uni-easyinput type="password" v-model="formData.confirmPassword" placeholder=""  @input="validatePassword"/>
					</uni-forms-item>
					<view v-if="errorMsg" class="error-msg">{{errorMsg}}</view>
				</uni-forms>
			</view>
			<view>
				<button type="primary" disabled="isValid" class="uni-button-color" @click="clickInter">保存修改</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { APIURL, post, postForm } from '../../utils/request.js';
	export default {
		data() {
			return {
				// 表单数据
				formData: {
					oldPassword: '',
					newPassword: '',
					confirmPassword: ''
				},
				isValid: true,
				errorMsg: '',
				userName: '',
				userCode: '',
				userId: ''
			}
		},
		onLoad() {
			
		},
		methods: {
			validatePassword() {
			      // 验证密码长度
			     /* if(this.form.newPassword.length < 6) {
			        this.errorMsg = '密码长度不能少于6位';
			        this.isValid = false;
			        return;
			      } */
			      // 验证两次密码是否一致
				  if(this.formData.confirmPassword) {
					  if(this.formData.newPassword !== this.formData.confirmPassword) {
					    this.errorMsg = '两次输入的密码不一致';
					    return;
					  }
				  }
			      if(this.formData.oldPassword) {
					  // 验证新旧密码是否相同
					  if(this.formData.oldPassword === this.formData.newPassword) {
					    this.errorMsg = '新密码不能与旧密码相同';
					    return;
					  }
				  }
			      this.errorMsg = '';
			      this.isValid = false;
				  console.log(this.isValid, 'this.isValid')
				  debugger
			    },
			clickInter(){
				uni.showLoading({
					title: '登录中'
				});
				if(!this.userCode || !this.userId){
					uni.showToast({
						icon: 'none',
						title: '请输入工号、密码'
					});
					return;
				}
				const params = {
				        number: this.userCode,
				        password: this.userId
				      };
				  post('/user/login', params)
				        .then(data => {
							uni.hideLoading();
							console.log(data,'data')
							if(data.code == 200) {
							    uni.hideLoading();
								this.getUser(data.data)
							} else {
								uni.showToast({
								  icon: 'none',
								  title: '登录失败，请检查正确用户信息！'
								});
							}
				        })
				        .catch(err => {
						  uni.hideLoading();
				          uni.showToast({
				            icon: 'none',
				            title: '登录失败，请检查接口返回'
				          });
				        });
			},
			getUser(userId) {
						  uni.hideLoading();
				postForm('/user/selectById', {id: userId} )
				    .then(data => {
						if(data.code == 200) {
							let userInfor = data.data
							userInfor.userId = userId,
							uni.setStorageSync('userInfor', userInfor);
							if(userInfor.isRectify == true) {
								uni.switchTab({
								    url: '/pages/work/index'
								});
							} else {
								uni.switchTab({
								    url: '/pages/index/index'
								});
							}
							
						} else {
							  uni.showToast({
							    icon: 'none',
							    title: "查询用户信息失败，请检查接口返回！"
							  });
						}
				    })
				    .catch(err => {
				        uni.showToast({
				           icon: 'none',
				          title: '获取数据失败，请检查接口返回'
				        });
				    });
			}
		}
	}
</script>

<style>
	.uni-button-color {
		background-color: #007aff !important;
		font-size: 17px !important;
	}
	.layout-form {
		text-align: center;
		padding: 0 20px;
		margin-top: 30px;
	}
	.error-msg {
	  color: red;
	  margin-bottom: 15px;
	  text-align: left;
	  margin-top: -20px;
	}
</style>
